const app = getApp();
var api = require('../../../../api.js');

Page({
  data: {
    value: 1,
    id: 0,
    // bgimg: '../../../../images/card.jpg',
    list:{
      card: '黄金会员卡',
      number: 9
    }
  },
  onLoad: function (options) {

    app.bind();
    var that = this;
    that.setData({
      id: options.id
    })

    that.getNumber();
  },
  onReady: function () {
  
  },
  onShow: function () {
  
  },
  onHide: function () {
  
  },
  onUnload: function () {
  
  },
  onPullDownRefresh: function () {
  
  },
  onReachBottom: function () {
  
  },
  //  确认转赠
  onShareAppMessage: function () {
    var that = this;
    wx.hideShareMenu();
    var url = "pages/user/card/receive/receive";
    var user_id = wx.getStorageSync('user_id');
    var id = that.data.id;
    console.log(url + "?id=" + id + "&user_id=" + user_id);
    return {
      path: url + "?id=" + id + "&user_id=" + user_id,
      title: '得道1314',
      imageUrl: 'https://tzhouzi.fxm365.com/addons/zjhj_mall/core/web/uploads/image/f4/goldCard1.jpg',
      success: function () {
        that.transferSend();
      }
    };
  },
  // 获取可转赠会员卡数量
  getNumber: function () {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    // var id = that.data.id;
    // console.log(id);
    wx.request({
      url:api.card.number,
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: that.data.id
      },
      success: function (e) {
        // console.log(e);
        var list = e.data.data;
        // console.log(list);
        if (e.data.code == 0) {
          that.setData({
            list: list
          })
        } else if (e.data.code == 1) {
          wx.navigateTo({
            url: '/pages/login/login'
          })
        } else {
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        }
      }
    });
  },
  // 确认转赠
  // transfer: function () {

  // },
  // 确认转赠
  transferSend: function () {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    // var id = that.data.id;
    // console.log(id);
    wx.request({
      url:api.card.transfer,
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: that.data.id,
        share_num: that.data.value,
      },
      success: function (e) {
        // console.log(e);
        var msg;
        if (e.data.code == 0) {
          msg = "转赠成功！"
        } else if (e.data.code == 1) {
          msg = "转赠失败！"
        }
        wx.showToast({
          title: msg,
          icon: 'none',
        });
      }
    });
  },
  //改变输入框内的数量
  numberBlur: function (e) {
    // console.log(e);
    var that = this;
    var maxNum = that.data.list.left_num;
    var num = e.detail.value;
    num = parseInt(num);
    // console.log(num);
    // console.log(isNaN(num));
    if (isNaN(num)){
      num = 1;
    } else {
      if (num <= 0){
        that.setData({
          value: 1,
        })
      } else if (num >= maxNum) {
        that.setData({
          value: maxNum
        })
      } else {
        that.setData({
          value: num
        })
      }
    }
    // console.log(that.data.value);
  },
})